home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH16 / TESTGRAB.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-10  |  3.7 KB  |  173 lines

  1.         .xlist
  2.         include     stdlib.a
  3.         includelib    stdlib.lib
  4.         matchfuncs
  5.         .list
  6.  
  7. dseg        segment    para public 'data'
  8.  
  9. ; Variables used to hold the number of shares bought/sold, a pointer to
  10. ; a string containing the buy/sell command, and a pointer to a string
  11. ; containing the company name.
  12.  
  13. Count        word    0
  14. CmdPtr        dword    ?
  15. CompPtr        dword    ?
  16.  
  17.  
  18. ; Some test strings to try out:
  19.  
  20. Cmd1        byte    "Buy 25 shares of apple stock",0
  21. Cmd2        byte    "Sell 50 shares of hp stock",0
  22. Cmd3        byte    "Buy 123 shares of dec stock",0
  23. Cmd4        byte    "Sell 15 shares of ibm stock",0
  24. BadCmd0        byte    "This is not a buy/sell command",0
  25.  
  26.  
  27. ; Patterns for the stock buy/sell command:
  28. ;
  29. ; StkCmd matches buy or sell and creates a parenthetical pattern
  30. ; that contains the string "buy" or "sell".
  31.  
  32. StkCmd        pattern    {sl_match2, buyPat, 0, skipspcs1}
  33.  
  34. buyPat        pattern    {matchistr,buystr,sellpat}
  35. buystr        byte    "BUY",0
  36.  
  37. sellpat        pattern    {matchistr,sellstr}
  38. sellstr        byte    "SELL",0
  39.  
  40. ; Skip zero or more white space characters after the buy command.
  41.  
  42. skipspcs1    pattern    {spancset, whitespace, 0, CountPat}
  43.  
  44. ; CountPat is a parenthetical pattern that matches one or more
  45. ; digits.
  46.  
  47. CountPat    pattern    {sl_match2, Numbers, 0, skipspcs2}
  48. Numbers        pattern    {anycset, digits, 0, RestOfNum}
  49. RestOfNum    pattern    {spancset, digits}
  50.  
  51. ; The following patterns match " shares of " allowing any amount
  52. ; of white space between the words.
  53.  
  54. skipspcs2    pattern    {spancset, whitespace, 0, sharesPat}
  55.  
  56. sharesPat    pattern    {matchistr, sharesStr, 0, skipspcs3}
  57. sharesStr    byte    "SHARES",0
  58.  
  59. skipspcs3    pattern    {spancset, whitespace, 0, ofPat}
  60.  
  61. ofPat        pattern    {matchistr, ofStr, 0, skipspcs4}
  62. ofStr        byte    "OF",0
  63.  
  64. skipspcs4    pattern    {spancset, whitespace, 0, CompanyPat}
  65.  
  66.  
  67.  
  68. ; The following parenthetical pattern matches a company name.
  69. ; The patgrab-available string will contain the corporate name.
  70.  
  71. CompanyPat    pattern    {sl_match2, ibmpat}
  72.  
  73. ibmpat        pattern    {matchistr, ibm, applePat}
  74. ibm        byte    "IBM",0
  75.  
  76. applePat    pattern    {matchistr, apple, hpPat}
  77. apple        byte    "APPLE",0
  78.  
  79. hpPat        pattern    {matchistr, hp, decPat}
  80. hp        byte    "HP",0
  81.  
  82. decPat        pattern    {matchistr, decstr}
  83. decstr        byte    "DEC",0
  84.  
  85.  
  86.         include    stdsets.a
  87. dseg        ends
  88.  
  89.  
  90. cseg        segment    para public 'code'
  91.         assume    cs:cseg, ds:dseg
  92.  
  93.  
  94. ; DoBuySell-    This routine processes a stock buy/sell command.
  95. ;        After matching the command, it grabs the components
  96. ;        of the command and outputs them as appropriate.
  97. ;        This routine demonstrates how to use patgrab to
  98. ;        extract substrings from a pattern string.
  99. ;
  100. ;        On entry, es:di must point at the buy/sell command
  101. ;        you want to process.
  102.  
  103. DoBuySell    proc    near
  104.         ldxi    StkCmd
  105.         xor    cx, cx
  106.         match
  107.         jnc    NoMatch
  108.  
  109.         lesi    StkCmd
  110.         patgrab
  111.         mov    word ptr CmdPtr, di
  112.         mov    word ptr CmdPtr+2, es
  113.  
  114.         lesi    CountPat
  115.         patgrab
  116.         atoi            ;Convert digits to integer
  117.         mov    Count, ax
  118.         free            ;Return storage to heap.
  119.  
  120.         lesi    CompanyPat
  121.         patgrab
  122.         mov    word ptr CompPtr, di
  123.         mov    word ptr CompPtr+2, es
  124.  
  125.         printf
  126.         byte    "Stock command: %^s\n"
  127.         byte    "Number of shares: %d\n"
  128.         byte    "Company to trade: %^s\n\n",0
  129.         dword    CmdPtr, Count, CompPtr
  130.  
  131.         les    di, CmdPtr
  132.         free
  133.         les    di, CompPtr
  134.         free
  135.         ret
  136.  
  137. NoMatch:    print
  138.         byte    "Illegal buy/sell command",cr,lf,0
  139.         ret
  140. DoBuySell    endp
  141.  
  142. Main        proc
  143.         mov    ax, dseg
  144.         mov    ds, ax
  145.         mov    es, ax
  146.  
  147.         meminit
  148.  
  149.         lesi    Cmd1
  150.         call    DoBuySell
  151.         lesi    Cmd2
  152.         call    DoBuySell
  153.         lesi    Cmd3
  154.         call    DoBuySell
  155.         lesi    Cmd4
  156.         call    DoBuySell
  157.         lesi    BadCmd0
  158.         call    DoBuySell
  159.  
  160. Quit:        ExitPgm
  161. Main        endp
  162.  
  163. cseg            ends
  164.  
  165. sseg        segment    para stack 'stack'
  166. stk        db    1024 dup ("stack   ")
  167. sseg        ends
  168.  
  169. zzzzzzseg    segment    para public 'zzzzzz'
  170. LastBytes    db    16 dup (?)
  171. zzzzzzseg    ends
  172.         end    Main
  173.